global with sharing class UpdateOutletToSMSWS {
    
    //更新nc并返回数据
    WebService static List<Account> getOutletListAndUpdate(List<Account> updateOutletList,DateTime dt)
    {
        List<Account> returnOutletList=new List<Account>();
        List<SynchronizeLog__c> synchList=new List<SynchronizeLog__c>();
        List<Account> updateList=new List<Account>();
        
        if(dt!=NULL)
        {
            Savepoint sp=DataBase.setSavepoint(); 
          try
           {
                dateTime dtInvoke=dateTime.now();
                //取出SFA中的要更新的门店信息返回给SMS
                returnOutletList=[Select  OUTLETREGION__C,OUTLETSUBREGION__C,OUTLETAREA__C,CITYNAME__C,
                                         OUTLETCODE__C,NAME,OUTLET_ADDRESS__C,CHANNEL__C,ACTIVE_STATUS__C,WITH_NC_OR_NOT__C
                                         ,Chain_Customer__c,LASTMODIFIEDDATE, SynchronizeTime__c        
                                     From Account
                                     where LastModifiedDate>:dt];
                                             
                //拿到SMS的NC门店数据更新到SFA
                if(updateOutletList!=NULL && updateOutletList.size()>0)
                {
                    for(Account acc:updateOutletList)
                    {
                        updateList.add(new Account(OutletCode__c=acc.OutletCode__c,With_NC_or_not__c=acc.With_NC_or_not__c, changeByApi__c=true));
                        //把SMS记录写到日志中
                        SynchronizeLog__c sycn=new SynchronizeLog__c(DataType__c='SMS',OutletCode__c=acc.OutletCode__c,WITH_NC_OR_NOT__C=acc.WITH_NC_OR_NOT__C,SynchronizeDateTime__c=dt, InvokeTime__c=dtInvoke);
                        synchList.add(sycn);
                    }
                    upsert updateList OutletCode__c;//将SMS数据更新至SFA
                    insert synchList;//更新日志
                    synchList.clear();
                }   
                
                //把返回出去的记录写到日志表中
                for(Account reAcc:returnOutletList)
                {
                    SynchronizeLog__c sycn=new SynchronizeLog__c();
                    sycn.DataType__c='SFA';
                    sycn.OutletCode__c=reAcc.OutletCode__c;
                    sycn.OutletId__c=reAcc.Id;
                    sycn.SynchronizeDateTime__c=dt;
                    sycn.OUTLETREGION__C=reAcc.OUTLETREGION__C;
                    sycn.OUTLETSUBREGION__C=reAcc.OUTLETSUBREGION__C;
                    sycn.OUTLETAREA__C=reAcc.OUTLETAREA__C;
                    sycn.CITYNAME__C=reAcc.CITYNAME__C;
                    sycn.OutletName__c=reAcc.Name;
                    sycn.OUTLET_ADDRESS__C=reAcc.OUTLET_ADDRESS__C;
                    sycn.CHANNEL__C=reAcc.CHANNEL__C;
                    sycn.ACTIVE_STATUS__C=reAcc.ACTIVE_STATUS__C;
                    sycn.WITH_NC_OR_NOT__C=reAcc.WITH_NC_OR_NOT__C;
                    sycn.Chain_Customer__c=reAcc.Chain_Customer__c;
                    sycn.InvokeTime__c=dtInvoke;
                    
                    synchList.add(sycn);
                }
                
                if(synchList.size()>0)
                    insert synchList;
                    
                //把当前系统时间写入记录中
                dateTime dtDone= dateTime.now();
                 for(Account acc:returnOutletList)
                {
                    acc.SynchronizeTime__c=dtDone;
                }    
               
               return returnOutletList;
            }
            catch(Exception ex)
            {
                Database.rollback(sp);
                throw ex;
           }
        }
        return returnOutletList;
    }
    
    //只返回数据
    WebService static List<Account> GetOutletList(DateTime dt)
    {
        List<Account> returnOutletList=new List<Account>();
        List<SynchronizeLog__c> synchList=new List<SynchronizeLog__c>();
 
        Savepoint sp=DataBase.setSavepoint(); 
         dateTime dtInvoke=dateTime.now();
         
        try
        {
            if(dt!=NULL)
            {
               
                //取出SFA中的要更新的门店信息返回给SMS             
                    returnOutletList=[Select OUTLETREGION__C,OUTLETSUBREGION__C,OUTLETAREA__C,CITYNAME__C,
                                         OUTLETCODE__C,NAME,OUTLET_ADDRESS__C,CHANNEL__C,ACTIVE_STATUS__C,WITH_NC_OR_NOT__C
                                         ,Chain_Customer__c,LASTMODIFIEDDATE,SynchronizeTime__c        
                                         From Account
                                         where LastModifiedDate>:dt limit 10000];
            }
            
            //把返回出去的记录写到日志表中
            for(Account reAcc:returnOutletList)
            {
                 SynchronizeLog__c sycn=new SynchronizeLog__c();
                    sycn.DataType__c='SFA';
                    sycn.OutletCode__c=reAcc.OutletCode__c;
                    sycn.OutletId__c=reAcc.Id;
                    sycn.SynchronizeDateTime__c=dt;
                    sycn.OUTLETREGION__C=reAcc.OUTLETREGION__C;
                    sycn.OUTLETSUBREGION__C=reAcc.OUTLETSUBREGION__C;
                    sycn.OUTLETAREA__C=reAcc.OUTLETAREA__C;
                    sycn.CITYNAME__C=reAcc.CITYNAME__C;
                    sycn.OutletName__c=reAcc.Name;
                    sycn.OUTLET_ADDRESS__C=reAcc.OUTLET_ADDRESS__C;
                    sycn.CHANNEL__C=reAcc.CHANNEL__C;
                    sycn.ACTIVE_STATUS__C=reAcc.ACTIVE_STATUS__C;
                    sycn.WITH_NC_OR_NOT__C=reAcc.WITH_NC_OR_NOT__C;
                    sycn.Chain_Customer__c=reAcc.Chain_Customer__c;
                     sycn.Chain_Customer__c=reAcc.Chain_Customer__c;
                     sycn.InvokeTime__c=dtInvoke;
                     
                synchList.add(sycn);
            }
            
            if(synchList.size()>0)
            insert synchList;
        }
        catch(Exception ex)
        {
            Database.rollback(sp);
            throw ex;
        } 
       //把系统时间写入记录中
         dateTime dtDone= dateTime.now();
         for(Account acc:returnOutletList)
         {
              acc.SynchronizeTime__c=dtDone;
         }    
              
        return returnOutletList;
    }
}